//
//  ZXViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/6/2.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class ZXViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,LgCallBack{
    var zxList = [ZXModel]()
    var pageNo = 1
    let header = MJRefreshNormalHeader()
    let footer = MJRefreshAutoNormalFooter()
    var destName = ""
    @IBOutlet weak var tbv: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tbv.layer.cornerRadius = 10
//        header.setRefreshingTarget(self, refreshingAction:#selector(self.headerRefresh))
//        self.tbv.mj_header = header
//        footer.setRefreshingTarget(self, refreshingAction:#selector(self.footerRefresh))
//        self.tbv.mj_footer = footer
        getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return zxList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath) as! ZXTableViewCell
        cell.zx = zxList[indexPath.row]
        return cell
    }
    
    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            zxList += parseZXModelList(all:j["MobileBody"].arrayValue)
        }
        tbv.reloadData()
        header.endRefreshing()
        footer.endRefreshing()

    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         destName = zxList[indexPath.row].classification
         performSegue(withIdentifier: "toQuestion", sender: nil)
         
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toQuestion"{
            let dest = segue.destination as! QuestionViewController
            dest.qtype = 3
            dest.classification = destName
            
        }
    }
    
    func getData(){
        asyn_req_post(method: M_GetClassification, cb: self, p: ["pageNo":pageNo as AnyObject,"pageSize":10 as AnyObject])
    }
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(1)
        zxList.removeAll()
        //重现生成数据
        pageNo = 1
        getData()
        
    }
    
    func footerRefresh(){
        print("提拉加载")
        sleep(1)
        pageNo += 1
        getData()
    }
    
}
